﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestMesh : MonoBehaviour {

    public Material mat;
    private int VERTICES_COUNT = 12;
    // Use this for initialization
    void Start () {

        var go = new GameObject("testMesh");
        go.transform.SetParent(GameObject.Find("Canvas").transform);
        go.transform.localScale = Vector3.one;
        go.transform.localPosition = new Vector3(-300, 140, 0);
        MeshFilter meshFilter = go.AddComponent<MeshFilter>();
        MeshRenderer meshRender = go.AddComponent<MeshRenderer>();
        go.layer = LayerMask.NameToLayer("UI");
        
        meshFilter.mesh = CreateCustomPlane(100, 100, 200, 200, true);//false;

        meshRender.material = mat;
        meshRender.sortingOrder = 1;
        meshRender.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
        meshRender.lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
        meshRender.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
        meshRender.receiveShadows = false;
    }

    private Mesh CreateCustomPlane(float width, float height, int nRow, int nCol, bool isAddRandom)
    {
        Mesh m = new Mesh();
        m.name = "customPlane";
        //note: unity is left-hand system
        Vector3 LU = new Vector3(-width / 2, height / 2, 0);
        float dx = width / nCol;
        float dy = height / nRow;
        int vertCount = (nRow + 1) * (nCol + 1);
        int triangleCount = nRow * nCol * 2;
        Vector3[] vertices = new Vector3[vertCount];
        Vector2[] uv = new Vector2[vertCount];
        int[] IDs = new int[triangleCount * 3];
        for (int i = 0; i < nRow + 1; i++)
        {
            for (int j = 0; j < nCol + 1; j++)
            {
                Vector3 v = LU + new Vector3(dx * j, -dy * i, 0);
                Vector2 texCoord = new Vector2(dx * j / width, 1 - dy * i / height);
                if (isAddRandom)
                {//add random
                    v.z = (Random.value * 50 - 1) * 0.1f;
                }
                vertices[i * (nCol + 1) + j] = v;
                uv[i * (nCol + 1) + j] = texCoord;

            }
        }
        for (int i = 0; i < nRow; i++)
        {
            for (int j = 0; j < nCol; j++)
            {
                //quad[i][j]
                int LUID = i * (nCol + 1) + j;
                int LDID = LUID + (nCol + 1);
                int RUID = LUID + 1;
                int RDID = LDID + 1;
                //quad[i][j] is triangle[2*(i*4+j)] and triangle[2*(i*4+j)+1] 
                //triangle[2*(i*4+j)] is {IDs[2*(i*4+j)*3],IDs[2*(i*4+j)*3+1],IDs[2*(i*4+j)*3+2]}
                IDs[2 * (i * nCol + j) * 3] = LUID;
                IDs[2 * (i * nCol + j) * 3 + 1] = RUID;
                IDs[2 * (i * nCol + j) * 3 + 2] = LDID;
                //triangle[2*(i*4+j)+1] is {IDs[(2*(i*4+j)+1)*3],IDs[(2*(i*4+j)*3+1)*3+1],IDs[(2*(i*4+j)*3+2)*3+2]}
                IDs[(2 * (i * nCol + j) + 1) * 3] = LDID;
                IDs[(2 * (i * nCol + j) + 1) * 3 + 1] = RUID;
                IDs[(2 * (i * nCol + j) + 1) * 3 + 2] = RDID;
            }
        }

        m.vertices = vertices;
        m.uv = uv;
        m.triangles = IDs;

        m.RecalculateNormals();
        return m;
    }
}
